/**
 * Created by Honghaiw on 2017/2/27.
 * 加载天地图切片类
 * 继承自TileLayer
 * @param {Object} tomcat中映射该切片目录url
 * @param {Object} options
 */

L.TileLayer.TdtTileLoad = L.TileLayer.extend({
    initialize: function (url, options) {
        var options = L.setOptions(this, options);
        //this.url = url+options.mapType+"/wmts?service=WMTS&request=GetTile&version=1.0.0&style=default&format=image/jpgpng&layer="+options.lyrType+"&TileMatrixSet=default028mm&TileMatrix={z}&TileCol={x}&TileRow={y}";
        this.url = url+options.mapType+"/service/WMTS?Service=WMTS&VERSION=1.0.0&Request=GetTile&Layer="+options.lyrType+"&Format=image/png&TileMatrixSet=TileMatrixSet0&TileMatrix={z}&Style=default&TileRow={y}&TileCol={x}";
        // http://srv.jhmap.gov.cn/ykemap    /service/WMTS?Service=WMTS&VERSION=1.0.0&Request=GetTile&Layer=YKEMAP    &Format=image/png&TileMatrixSet=TileMatrixSet0&TileMatrix=" + level + "&Style=default&TileRow=" + row + "&TileCol=" + col;
        // http://srv.jhmap.gov.cn/ykemapanno/service/WMTS?Service=WMTS&VERSION=1.0.0&Request=GetTile&Layer=YKEMAPANNO&Format=image/png&TileMatrixSet=TileMatrixSet0&TileMatrix=" + level + "&Style=default&TileRow=" + row + "&TileCol=" + col;
        // http://srv.jhmap.gov.cn/YKIMG     /service/WMTS?Service=WMTS&VERSION=1.0.0&Request=GetTile&Layer=YKIMG     &Format=image/png&TileMatrixSet=TileMatrixSet0&TileMatrix=" + level + "&Style=default&TileRow=" + row + "&TileCol=" + col;
        // http://srv.jhmap.gov.cn/YKIMGANNO /service/WMTS?Service=WMTS&VERSION=1.0.0&Request=GetTile&Layer=YKIMGANNO &Format=image/png&TileMatrixSet=TileMatrixSet0&TileMatrix=" + level + "&Style=default&TileRow=" + row + "&TileCol=" + col;

        L.TileLayer.prototype.initialize.call(this, this.url, options);
    }
});

/**
 * 重写TileLayer中获取切片url方法
 * @param {Object} tilePoint
 */
L.TileLayer.TdtTileLoad.prototype.getTileUrl = function(tilePoint) {
    return L.Util.template(this._url, L.extend({
        s: this._getSubdomain(tilePoint),
        n: function(){
            var value=Math.floor(Math.random()*8);
            return value;
        },
        z: function() {
            var value = tilePoint.z.toString(10);
            return value;
        },
        x: function() {
            var value = tilePoint.x.toString(10);
            return value;
        },
        y: function() {
            var value = tilePoint.y.toString(10);
            return value;
        }
    }));
};


// 自定义瓦片图层类
var TdtTileLayer = function(options){
    var defaults = {
        //imgUrl:'http://srv{n}.zjditu.cn/',
        imgUrl:'http://srv.jhmap.gov.cn/',
        tileSize:256
    };

    this.opts = L.extend(defaults,options);

    switch(this.opts.mapType)
    {
        // case 'ZJEMAP_2D':
        //     this.opts.lyrType="TDT_ZJEMAP";
        //     break;
        // case 'ZJEMAPANNO_2D':
        //     this.opts.lyrType="TDT_ZJEMAPANNO";
        //     break;
        // case 'ZJDOM_2D':
        //     this.opts.lyrType="imgmap";
        //     break;
        // case 'ZJDOMANNO_2D':
        //     this.opts.lyrType="TDT_ZJIMGANNO";
        //     break;
        case 'ykemap':
            this.opts.lyrType="YKEMAP";
            break;
        case 'ykemapanno':
            this.opts.lyrType="YKEMAPANNO";
            break;
        case 'YKIMG':
            this.opts.lyrType="YKIMG";
            break;
        case 'YKIMGANNO':
            this.opts.lyrType="YKIMGANNO";
            break;
        default:
            this.opts.lyrType="";
            break;
    }

    this.tileLayer =  new L.TileLayer.TdtTileLoad(this.opts.imgUrl, {
        maxZoom: this.opts.maxZoom,
        minZoom: this.opts.minZoom,
        tileSize:this.opts.tileSize,
        continuousWorld: true,
        mapType:this.opts.mapType,
        lyrType: this.opts.lyrType,
        opacity:1.0
    });
};

L.tileLayer.tdtTileLoad = function(url, options){
    return new L.TileLayer.TdtTileLoad(url, options);
};

/**
 * 高位补全方法
 * @param {Object} 数字类型字符串
 * @param {Object} 总位数，不足则高位补0
 */
var pad = function(numStr, n) {
    var len = numStr.length;
    while(len < n) {
        numStr = "0" + numStr;
        len++;
    }
    return numStr;
};


